package lanou.giftsay_01.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextClock;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import lanou.giftsay_01.R;
import lanou.giftsay_01.bean.BodyBean;
import lanou.giftsay_01.tools.BaseActivity;

/**
 * Created by dllo on 16/8/27.
 */
public class BodyLvAdapter  extends BaseAdapter{
    private BodyBean bodyBean ;
    private Context context;

    public void setBodyBean(BodyBean bodyBean) {
        this.bodyBean = bodyBean;
        notifyDataSetChanged();
    }

    public BodyLvAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return bodyBean != null ? bodyBean.getData().getPosts().size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
    if (convertView  == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.item_bodylv ,parent,false);
        viewHolder = new ViewHolder(convertView);
        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }
        viewHolder.body.setText(bodyBean.getData().getPosts().get(position).getTitle());
        Picasso.with(context).load(bodyBean.getData().getPosts().get(position).getCover_image_url()).into(viewHolder.imageView);
        viewHolder.nickName.setText(bodyBean.getData().getPosts().get(position).getAuthor().getNickname());
        viewHolder.count.setText(String.valueOf(bodyBean.getData().getPosts().get(position).getLikes_count()));
        return convertView;
    }

    private class ViewHolder {
        TextView body ;
        TextClock textClock;
        ImageView imageView;
        TextView  nickName ;
        TextView count;

        public ViewHolder(View view) {
            body = (TextView) view.findViewById(R.id.item_body_tv);
            textClock = (TextClock) view.findViewById(R.id.item_body_tc);
            imageView = (ImageView) view.findViewById(R.id.iv_body_item);
            nickName = (TextView) view.findViewById(R.id.item_body_nickname);
            count = (TextView) view.findViewById(R.id.item_body_count);
        }
    }
}
